package com.macro.mall.portal.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * MyBatis相关配置
 * Created by macro on 2019/4/8.
 */
@Configuration
@EnableTransactionManagement
@MapperScan({"com.macro.mall.mapper",
        "com.macro.mall.portal.dao",
        "com.quyum.mall.market.mapper",
        "com.example.dao",
        "com.gitee.sunchenbin.mybatis.actable.dao"})
@ComponentScan(value = {"com.quyum.mall.market.*"})
public class MyBatisConfig {

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public YamlPropertiesFactoryBean configProperties() throws Exception{
        YamlPropertiesFactoryBean propertiesFactoryBean = new YamlPropertiesFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        propertiesFactoryBean.setResources(resolver.getResources("classpath*:application.yml"));
        return propertiesFactoryBean;
    }

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(30);
        dataSource.setInitialSize(10);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource());
        return dataSourceTransactionManager;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        List<Resource> resourcesList = new ArrayList<>();
        Resource[] r1 = resolver.getResources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml");
        Resource[] r2 = resolver.getResources("classpath*:com.quyum.mall.market.mapping/*.xml");
        Resource[] r3 = resolver.getResources("classpath:dao/*.xml");
        Resource[] r4 = resolver.getResources("classpath*:com/macro/mall/mapper/*.xml");
        resourcesList.addAll(Arrays.asList(r1));
        resourcesList.addAll(Arrays.asList(r2));
        resourcesList.addAll(Arrays.asList(r3));
        resourcesList.addAll(Arrays.asList(r4));
        Resource[] r7 = new Resource[resourcesList.size()];
        Resource[] resources = resourcesList.toArray(r7);
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.example.entity.*");
        return sqlSessionFactoryBean;


    }

    public static String getDefaultMemberIcon(){
        return "https://mei-rang.oss-cn-beijing.aliyuncs.com/img/2020/10/28/8c1ad68c572d4a33a3466b5e5eaf92d5579d5a59-d41f-4578-bd22-0c908b6153df.jpg";
    }
}
